<template>
    <div class="main-container">
        <div class="query_area" v-if="dialogVisible">
            <el-card class="box-card">
                <div slot="header" class="clearfix">
                    <span><i class="el-icon-condition"></i></span>
                </div>
                <div style="height: 40px;">
                    <el-form :inline="true" :model="formQuery">
                        <el-form-item label="名称">
                            <el-input size="small" v-model="formQuery.name" placeholder="名称"></el-input>
                        </el-form-item>
                        <el-form-item label="发起人">
                            <el-input size="small" v-model="formQuery.category" placeholder="发起人"></el-input>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select size="small" v-model="formQuery.status" placeholder="请选择">
                                <el-option label="全部" value=""></el-option>
                                <el-option label="已发布" value="1"></el-option>
                                <el-option label="未发布" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="onSubmit(null,null)" size="small">查询
                            </el-button>
                            <el-button type="info" icon="el-icon-refresh-left" @click="onReset" size="small">
                                重置
                            </el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-card>
            <el-card class="table-list">
                <div slot="header" class="clearfix">
                    <span><i class="el-icon-table-list" style="font-weight: 600;margin-top: 8px;"></i></span>
                    <el-button class="el-icon-suitcase" style="float: right; padding: 3px 0;font-size: 20px" type="text"
                               @click="handleClick(null)"></el-button>
                </div>
                <div>
                    <el-table :data="tableData" height="calc(100vh - 400px)" border style="width: 100%"
                              empty-text="暂无数据" :loading="loading">
                        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
                        <el-table-column align="center" prop="name" label="流程名称"></el-table-column>
                        <el-table-column align="center" width="120" prop="category" label="当前节点"></el-table-column>
                        <el-table-column align="center" prop="metaInfo" label="发起人"></el-table-column>
                        <el-table-column align="center" prop="createTime" label="发起时间" width="120"
                                         :formatter="formatterDate"></el-table-column>
                        <el-table-column align="center" prop="createTime" label="办理时间" width="120"
                                         :formatter="formatterDate"></el-table-column>
                        <el-table-column align="center" prop="status" label="流程状态" width="100">
                            <template slot-scope="scope">
                                <span v-if="scope.row.deploymentId" disabled>已发布</span>
                                <span v-else>未发布</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" fixed="right" label="操作" width="200">
                            <template slot-scope="scope">
                                <el-button type="text" @click="handleViewFlowImg(scope.row)" style="font-size: 12px">
                                    执行路径
                                </el-button>
                                <el-button @click="handleClick(scope.row)" type="text" style="font-size: 12px">
                                    办理历史
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div style="text-align: right;margin-top: 8px">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :current-page="currentPage"
                                   :page-sizes="[10, 20, 50, 100]"
                                   :page-size="pageSize"
                                   layout="prev, pager, next, sizes,total"
                                   :total="total">
                    </el-pagination>
                </div>
            </el-card>
        </div>
        <edit-workflow v-else :dialogVisible="dialogVisible" :editTitle="editTitle" :rowData="rowData"
                       @callBackFromSub="callBackFromSub"></edit-workflow>
        <el-dialog title="流程模型流程图" :visible.sync="imgDialogVisible" width="50%">
            <el-image :src="flowImgUrl"></el-image>
        </el-dialog>
    </div>
</template>

<script>
    import EditWorkflow from "../workflow/EditWorkflow"

    export default {
        name: "CopyToMe",
        data() {
            return {
                tableData: [],
                rowData: {},
                currentPage: 1,
                pageSize: 10,
                total: 0,
                loading: false,
                formQuery: {
                    name: null,
                    category: null,
                    status: null
                },
                editTitle: "提示",
                dialogVisible: true,
                imgDialogVisible: false,
                flowImgUrl: null,
                categories: [
                    {
                        value: "apply holiday",
                        label: "请假"
                    },
                    {
                        value: "bx",
                        label: "报销"
                    }
                ]
            }
        },
        components: {
            EditWorkflow
        },
        methods: {
            //查看流程图
            handleViewFlowImg(param) {
                this.imgDialogVisible = !!param;
                this.flowImgUrl = this.$flowUrlHttp + "api/rest/model/img/" + param.id;
            },
            formatterDate(row, column, cellValue, index) {
                return this.$formatDate(new Date(row.createTime), 'yyyy-MM-dd');
            },
            handleDeployed(row) {
                this.$httpPost(this.$axiosFlowHttp, "api/rest/model/deploy/" + row.id, {}).then(res => {
                    if (res.code === 200) {
                        this.$successMsg("部署成功");
                    } else {
                        this.$errorMsg("部署失败");
                    }
                    this.onSubmit(null, null);
                });
            },
            handleClick(row) {
                this.dialogVisible = false;
                if (row) {
                    this.editTitle = "编辑模型";
                    this.rowData = row;
                } else {
                    this.editTitle = "添加模型";
                    this.rowData = {};
                }
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.onSubmit(null, val);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.onSubmit(val, this.pageSize);
            },
            //查询方法
            onSubmit(currentPage, pageSize) {
                this.formQuery.currentPage = currentPage ? currentPage : 1;
                this.formQuery.pageSize = pageSize ? pageSize : this.pageSize;
                this.initTableData(this.formQuery);
            },
            onReset() {
                this.formQuery = {
                    name: null,
                    category: null,
                    status: null
                };
            },
            handleDelete(row) {
                this.$httpDelete(this.$axiosFlowHttp, "api/rest/model/del/" + row.id, {}).then(res => {
                    if (res.code === 200) {
                        this.$successMsg("删除成功");
                    } else {
                        this.$errorMsg("删除失败");
                    }
                    this.onSubmit(null, null);
                });
            },
            //子组件方法回调
            callBackFromSub(param) {
                this.dialogVisible = param;
                this.onSubmit(null, null);
            },
            //初始化表格数据
            initTableData(param) {
                this.loading = true;
                this.$httpGet(this.$axiosFlowHttp, "api/rest/model/list/page", param).then(res => {
                    if (res.code === 200) {
                        this.total = res.total;
                        this.pageSize = res.pageSize;
                        this.tableData = res.data;
                    }
                    this.loading = false;
                });
            }
        },
        mounted() {
            this.onSubmit(null, null);
        }
    }
</script>

<style scoped lang="scss">
    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }

    .clearfix:after {
        clear: both
    }

    .clearfix > span {
        display: inline-block;
        height: 30px;
        line-height: 15px;
    }

    .el-icon-condition, .el-icon-table-list {
        font-size: 20px;
        margin: 3px 3px 3px 3px;
    }

    .table-list {
        margin-top: 10px;
        /*height: calc(100vh);*/
    }

    .el-card {
        /deep/ .el-card__header {
            padding: 10px 15px;
        }
    }

    .avatar {
        width: 35px;
        height: 35px;
        border-radius: 35px;
    }

    .el-table .cell {
        height: 23px;

        .el-button {
            padding: 0 0;
        }
    }
</style>
